
var buildTree = function (inorder, postorder) {
  if (!postorder.length) return null;
  let rootVal = postorder[postorder.length - 1];
  let node = new TreeNode(rootVal);
  let i = 0;
  for (; i < inorder.length; i++) {
    if (inorder[i] == rootVal) break;
  }

  node.left = buildTree(inorder.slice(0, i), postorder.slice(0, i));
  node.right = buildTree(inorder.slice(i + 1), postorder.slice(i, postorder.length - 1));
  return node;
}